#include "offermanagermainwindow.h"
#include "ui_offermanagermainwindow.h"
#include "global.h"
#include <QMainWindow>
#include <QMessageBox>
#include <QRegularExpression>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QSqlRecord>
#include <QSqlField>
#include <QVector>
#include "companymanagement.h"
#include "jobmanagement.h"
#include "processmanagementdialog.h"

OfferManagerMainWindow::OfferManagerMainWindow(QWidget *parent) :
    QMainWindow(parent),
    companyManagement(nullptr),
//    jobManagement(nullptr),
    ui(new Ui::OfferManagerMainWindow),
    database(QSqlDatabase::addDatabase("QSQLITE"))
{
    database.setDatabaseName(Global::DATABASE_NAME);
    if (!database.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database connection.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
    }

    this->ui->setupUi(this);
    this->ui->insertDeleteButtonFrame->setVisible(false);

    this->refreshDatabaseTables();

    companyManagement = new CompanyManagement(database, this, Qt::Dialog);
    connect(this->ui->companyAction, QAction::triggered, companyManagement, CompanyManagement::show);    
    connect(this->ui->jobAction, QAction::triggered, this, [&](){(new JobManagement(database, this))->exec();});
    connect(this->ui->processAction, QAction::triggered, this, [&](){(new ProcessManagementDialog(database, this))->exec();});
    connect(this->ui->refreshAction, QAction::triggered, this, [&](){this->refreshDatabaseTables();this->onTreeWidgetCurrentItemChanged(this->ui->treeWidget->currentItem(), nullptr);});
}

OfferManagerMainWindow::~OfferManagerMainWindow()
{
    delete ui;
}

void OfferManagerMainWindow::refreshDatabaseTables()
{
    this->databaseTables.clear();
    for (QSql::TableType tableType : {QSql::Tables, QSql::Views})
    {
        QTreeWidgetItem *tableCategory = new QTreeWidgetItem(this->ui->treeWidget);
        if (tableType == QSql::Tables)
            tableCategory->setText(0, "表");
        else
            tableCategory->setText(0, "视图");
        for (const QString &table : database.tables(tableType))
            if (table.left(6).toLower() != QString("sqlite"))
            {
                QTreeWidgetItem *tableItem = new QTreeWidgetItem(tableCategory);
                tableItem->setText(1, table);
                QSqlTableModel *model = new QSqlTableModel(this, database);
                model->setTable(table);
                model->select();
                connect(model, QSqlTableModel::dataChanged, this, OfferManagerMainWindow::onUpdateRecord);
                this->databaseTables.insert(table, model);
            }
    }
}

void OfferManagerMainWindow::onTreeWidgetCurrentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem*)
{
    auto it = databaseTables.find(current->text(1));
    if (it != databaseTables.end())
    {
        this->ui->tableView->setModel(databaseTables[current->text(1)]);
        this->ui->insertDeleteButtonFrame->setVisible(current->parent()->text(0) == QString("表"));
    }
}

void OfferManagerMainWindow::onInsertRecord()
{
    QSqlTableModel *model = this->tableViewModel();
    model->insertRecord(-1, model->record());
    model->select();
}
void OfferManagerMainWindow::onDeleteRecord()
{
    if (QMessageBox::warning(this, "警告","确定要删除选定行？",QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok)
    {
        QSqlTableModel *model = this->tableViewModel();
        model->removeRows(ui->tableView->currentIndex().row(), 1);
        model->select();
    }
}

void OfferManagerMainWindow::onUpdateRecord()
{
    this->tableViewModel()->submit();
}

QSqlTableModel *OfferManagerMainWindow::tableViewModel()
{
    return dynamic_cast<QSqlTableModel*>(ui->tableView->model());
}
